﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Microsoft.Telco.Web.Services.ServiceModel.Dispatcher;

namespace Microsoft.Telco.Web.Services.ServiceModel.Description.Behaviors
{
	/// <summary>
	/// Custom behavior to manage setting response codes for faults so that Silverlight clients can manage appropriately.
	/// </summary>
	public class SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
	{

		/// <summary>
		/// Implements a modification or extension of the service across an endpoint.
		/// </summary>
		/// <param name="endpoint">The endpoint that exposes the contract.</param>
		/// <param name="endpointDispatcher">The endpoint dispatcher to be modified or extended.</param>
		public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
		{
			// Add the message inspector so that we can correctly set HTTP return codes for Silverlight clients.
			endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new SilverlightFaultMessageInspector());
		}

		/// <summary>
		/// Implement to pass data at runtime to bindings to support custom behavior.
		/// </summary>
		/// <param name="endpoint">The endpoint to modify.</param>
		/// <param name="bindingParameters">The objects that binding elements require to support the behavior.</param>
		public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
		{
			// No-op
		}

		/// <summary>
		/// Implements a modification or extension of the client across an endpoint.
		/// </summary>
		/// <param name="endpoint">The endpoint that is to be customized.</param>
		/// <param name="clientRuntime">The client runtime to be customized.</param>
		public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
		{
			// No-op
		}

		/// <summary>
		/// Implement to confirm that the endpoint meets some intended criteria.
		/// </summary>
		/// <param name="endpoint">The endpoint to validate.</param>
		public void Validate(ServiceEndpoint endpoint)
		{
			// No-op
		}

		/// <summary>
		/// Gets the type of behavior.
		/// </summary>
		/// <value></value>
		/// <returns>A <see cref="T:System.Type"/>.</returns>
		public override System.Type BehaviorType
		{
			get { return typeof(SilverlightFaultBehavior); }
		}

		/// <summary>
		/// Creates a behavior extension based on the current configuration settings.
		/// </summary>
		/// <returns>The behavior extension.</returns>
		protected override object CreateBehavior()
		{
			return new SilverlightFaultBehavior();
		}
	}
}
